package com.xyauto.theme.base

import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xyauto.theme.base.internal.IBaseListView
import com.xyauto.theme.base.vm.BaseViewModel
import com.xyauto.theme.utils.AutoUtils

/**
 * Created by gongziyi on 2021/5/13 17:58.
 */
abstract class BaseListActivity<Adapter : BaseQuickAdapter<T, *>, T, VM : BaseViewModel> :
    BaseVMActivity<VM>(), IBaseListView<Adapter, T> ,OnRefreshLoadMoreListener{


    protected val mAdapter: Adapter by lazy {
        getAdapter()
    }

    protected val mRecyclerView: RecyclerView? by lazy {
        findViewById<RecyclerView>(getRecyclerviewId())
    }

    private val mRefreshLayout: SmartRefreshLayout? by lazy {
        findViewById<SmartRefreshLayout>(getRefreshLayoutId())
    }

    open val pageSize = 10
    private var pagerNumber = 1


    protected open fun getAdapter(): Adapter =
        getAdapterClass()?.newInstance() ?: throw IllegalArgumentException("adapter == null")

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initRv()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) = onRefresh()

    override fun onLoadMore(refreshLayout: RefreshLayout) = onLoadMore()

    //用于子类手动刷新
    open fun onRefresh() {
        pagerNumber = 1
        onLoadData(true, pagerNumber)
    }

    //用于子类手动控制加载
    open fun onLoadMore() {
        pagerNumber++
        onLoadData(false, pagerNumber)
    }

    protected open fun initRv() {
        mRefreshLayout?.let {
            it.setEnableLoadMore(true)
            it.setEnableRefresh(true)
            it.setOnRefreshListener(this)
            it.setOnLoadMoreListener(this)
        }
        mRecyclerView?.let {
            it.layoutManager = newLayoutManager(this)
            it.adapter = mAdapter
        }
        getEmptyLayoutId()?.let {
            val emptyView = LayoutInflater.from(this).inflate(it, null, false)
            AutoUtils.auto(emptyView)
            mAdapter.setEmptyView(emptyView)
        }
    }

    override fun onResume() {
        super.onResume()
        mAdapter.notifyDataSetChanged()
    }

    override fun onSuccessList(list: MutableList<T>?, hasNext: Boolean) {
        if (pagerNumber == 1) {
            mAdapter.setList(list)
        } else {
            mAdapter.addData(list ?: emptyList())
        }
        mRefreshLayout?.let {
            it.finishLoadMore()
            it.finishRefresh()
            it.setNoMoreData(!hasNext)
        }
    }

    override fun onFailureList() {
        mRefreshLayout?.let {
            it.finishLoadMore(false)
            it.finishRefresh(false)
            it.setNoMoreData(true)
        }
    }


}


